import { resolve } from 'node:path';
import uni from '@dcloudio/vite-plugin-uni';
import AutoImportTypes from 'auto-import-types';
import PiniaAutoRefs from 'pinia-auto-refs';
import Unocss from 'unocss/vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import {defineConfig, loadEnv} from 'vite';
import useRemoveConsole from 'vite-plugin-remove-console'

const isProduction = process.env.NODE_ENV === 'production'


// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd(), '')
	console.log(env)
	// const proxyURL = env.VITE_APP_API_URL

	return {
		resolve: {
			alias: {
				'@': resolve(__dirname, 'src')
			}
		},
		plugins: [
			AutoImportTypes(),
			PiniaAutoRefs(),
			AutoImport({
				dts: 'src/auto-imports.d.ts',
				imports: [
					'vue',
					'uni-app',
					'pinia',
					{
						'@/helper/pinia-auto-refs': ['useStore']
					}
				],
				exclude: ['createApp'],
				eslintrc: {
					enabled: true
				}
			}),
			Components({
				extensions: ['vue'],
				dts: 'src/components.d.ts'
			}),
			uni(),
			Unocss(),
			// ...(isProduction ? [useRemoveConsole()] : []),
		],
		server: {
			open: false, // 自动打开
			base: './ ', // 生产环境路径
			proxy: {
				// 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
				// 正则表达式写法
				// '^/api': {
				//   target: env.apiBaseUrl, // 后端服务实际地址
				//   changeOrigin: true, // 开启代理
				//   rewrite: (path) => path.replace(/^\/api/, '')
				// }
			}
		}
	}
});
